<?php

namespace App\Http\Controllers;

use App\Http\Controllers\OATrait\OATrait;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Resources\Json\JsonResource;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    use OATrait;



    /**
     * 成功返回结果
     *
     * @param array $data
     */
    protected function SucRes($data = [])
    {
        if($data instanceof AnonymousResourceCollection){
            $additional = $data->additional;
            $data = $data->resource->toArray();
            if (array_key_exists('data', $data)) {
                $data['list'] = $data['data'];
                unset($data['data']);
            }

            if($additional){
                foreach ($additional as $key => $value){
                    $data[$key] = $value;
                }
            }

        }

        if($data instanceof JsonResource){
            $data = $data->toArray(request());
        }

        if(is_object($data)){
            $data = json_decode(json_encode($data,JSON_UNESCAPED_UNICODE),true);
            //dd($data);
            $data = $data['original']['data'] ?? [];
        }

        if(isset($data['code']) && $data['code'] != 'ok') {
            return $this->error($data['msg']);
        }
        return $this->returnJson('ok', 'success', $data);
    }
    /**
     * 返回结果操作
     * @param int $code
     * @param string $msg
     * @param array $res
     */
    public function returnJson($state = 0, $msg = 'success', $data = null)
    {
        return response()->json(compact('state','msg','data'));
    }

    /**
     * 失败后返回值
     * @param string $code
     * @param string $msg
     */
    protected function error($msg = '')
    {
        return json_encode([
            'state' => 'err',
            'message' => $msg,
            'data' => (object)array(),  //这里返回一个空的对象
        ], JSON_UNESCAPED_UNICODE);
    }

    /**
     * Resource格式化
     *
     * @param $data
     * @return array
     */
    public function formatData($data)
    {
        if ($data instanceof JsonResource) {
            if ($data instanceof AnonymousResourceCollection) {
                $data = (array)($data->response()->getData());
            } else {
                $data = $data->toArray(request());
            }
        }

        if (!empty($data['meta'])) {
            $data['current_page'] = $data['meta']->current_page??1;
            $data['per_page'] = $data['meta']->per_page??10;
            $data['total'] = $data['meta']->total??0;
            unset($data['meta']);
        }
        if (!empty($data['links'])) {
            unset($data['links']);
        }
        if (isset($data['data'])) {
            $data['list'] = $data['data'];
            unset($data['data']);
        }

        return $data;
    }

    protected function success($msg = '',$data=[])
    {
        return json_encode([
            'state' => 'ok',
            'message' => $msg,
            'data' => $data,  //这里返回一个空的对象
        ], JSON_UNESCAPED_UNICODE);
    }




}
